package {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;

public class FactoryClass extends MovieClip {

	private var timer_count : int = 0;
	private var bar_prop : Object = [];
	private const CLASS_NAME : String = "Launcher";

	public function FactoryClass() {
		stop();
		setBarBounds();
		setStageProperties();
		startTimer();
	}

	private function setBarBounds() : void {
		bar_prop.x = 0;
		bar_prop.y = stage.stageHeight / 2 - 10;
		bar_prop.w = stage.stageWidth / 100;
		bar_prop.h = 20;
	}

	private function setStageProperties() : void {
		stage.scaleMode = StageScaleMode.NO_SCALE;
		stage.align = StageAlign.TOP_LEFT;
	}

	private function startTimer() : void {
		var timer : Timer = new Timer(20, 100);
		timer.addEventListener(TimerEvent.TIMER, onTimer);
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
		timer.start();
	}

	private function onTimer(event : TimerEvent) : void {
		drawProgress();
		updateCounter();
	}

	private function drawProgress() : void {
		graphics.beginFill(0);
		graphics.drawRect(bar_prop.x, bar_prop.y, bar_prop.w, bar_prop.h);
		graphics.endFill();
	}

	private function updateCounter() : void {
		timer_count ++;
		bar_prop.x = timer_count * stage.stageWidth / 100;
	}

	private function onTimerComplete(event : TimerEvent) : void {
		if (event.target is Timer) {
			event.target.removeEventListener(TimerEvent.TIMER, onTimer);
			event.target.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			event.target.stop();
		}
		graphics.clear();
		nextFrame();
		init();
	}

	private function init() : void {
		var main_class : Class = Class(getDefinitionByName(CLASS_NAME));
		if (main_class) {
			var app : DisplayObject = new main_class();
			addChild(app);
		}
	}
}
}
